﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Microsoft.Expression.Encoder.Devices;
using Microsoft.Expression.Encoder.Live;
using Microsoft.Expression.Encoder;

namespace GaraOto
{
    public partial class frmSetting : DevExpress.XtraEditors.XtraForm
    {
        public static EncoderDevice video = null;
        public static EncoderDevice audio = null;
        public static Boolean checkConfig = false;
        public static string videoDevice =null;
        public static string audioDevice = null;
        /// <summary>
        /// Creates job for capture of live source
        /// </summary>
        private LiveJob _job;

        /// <summary>
        /// Device for live source
        /// </summary>
        private LiveDeviceSource _deviceSource;

        private bool _bStartedRecording = false;
        public frmSetting()
        {
            InitializeComponent();
        }

        private void frmSetting_Load(object sender, EventArgs e)
        {
            lbcVideoDevices.Items.Clear();
            foreach (EncoderDevice edv in EncoderDevices.FindDevices(EncoderDeviceType.Video))
            {
                lbcVideoDevices.Items.Add(edv.Name);
            }
            lblVideoDevice.Text = "";

            lbcAudioDevices.Items.Clear();
            foreach (EncoderDevice eda in EncoderDevices.FindDevices(EncoderDeviceType.Audio))
            {
                lbcAudioDevices.Items.Add(eda.Name);
            }
            lblAudioDevice.Text = "";    
        }

        private void GetSelectedVideoAndAudioDevices(out EncoderDevice video, out EncoderDevice audio)
        {
            video = null;
            audio = null;

            lblAudioDevice.Text = "";
            lblVideoDevice.Text = "";

            if (lbcVideoDevices.SelectedIndex < 0 || lbcAudioDevices.SelectedIndex < 0)
            {
                MessageBox.Show("No Video and Audio capture devices have been selected.\nSelect an audio and video devices from the listboxes and try again.", "Warning");
                return;
            }

            // Get the selected video device            
            foreach (EncoderDevice edv in EncoderDevices.FindDevices(EncoderDeviceType.Video))
            {
                if (String.Compare(edv.Name, lbcVideoDevices.SelectedItem.ToString()) == 0)
                {
                    video = edv;
                    lblVideoDevice.Text = edv.Name;
                    videoDevice = edv.Name;
                    break;
                }
            }

            // Get the selected audio device            
            foreach (EncoderDevice eda in EncoderDevices.FindDevices(EncoderDeviceType.Audio))
            {
                if (String.Compare(eda.Name, lbcAudioDevices.SelectedItem.ToString()) == 0)
                {
                    audio = eda;
                    lblAudioDevice.Text = eda.Name;
                    audioDevice = eda.Name;
                    break;
                }
            }
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            GetSelectedVideoAndAudioDevices(out video, out audio);
            frmInputManager frm = (frmInputManager)this.Owner;
            frm.Begin();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ceConfiguration_CheckedChanged(object sender, EventArgs e)
        {
            checkConfig = ceConfiguration.Checked;
        }
    }
}